package com.top.mall.main.access

import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.dialog.LoadingDialog
import com.top.mall.jetpack.vm.AccessViewModel
import kotlinx.android.synthetic.main.activity_access.*

class AccessActivity : OwnerViewModeActivity<AccessViewModel>() {

    companion object{
        fun open(){
            ActivityUtils.startActivity(AccessActivity::class.java)
        }
    }

    override val layoutResId: Int
        get() = R.layout.activity_access

    override fun viewModel(): AccessViewModel {
        return createViewModel()
    }

    override fun stateBar(): StatusBar {
        return StatusBar(view = frameLayout)
    }


    override fun initView() {

        imageViewBack.setOnClickListener { finish() }

        supportFragmentManager(
            AccessLoginPwdFragment.newInstance(), isToBackStack = false
        )

    }

    override fun onAttachFragment(fragment: Fragment) {
        super.onAttachFragment(fragment)
        this.fillDataFragment(fragment)
    }

    override fun onBackPressed() {
        super.onBackPressed()
        supportFragmentManager.findFragmentById(R.id.frame_holder_access)?.let {
            this.fillDataFragment(it)
        }
    }

    private fun fillDataFragment(fragment: Fragment) {
        when (fragment) {
            is AccessLoginPwdFragment -> {
                text_access_title.setText(R.string.app_access_login)
                image_access_back.visibility = View.GONE
                linearLayout.visibility = View.VISIBLE
            }
            is AccessLoginCheckFragment -> {
                text_access_title.setText(R.string.app_access_login_phone)
                image_access_back.visibility = View.VISIBLE
                linearLayout.visibility = View.VISIBLE
            }
            is AccessRegisterFragment -> {
                text_access_title.setText(R.string.app_access_register)
                image_access_back.visibility = View.VISIBLE
                linearLayout.visibility = View.VISIBLE
            }
            is AccessCheckCodeFragment -> {
                text_access_title.setText(R.string.app_access_captcha)
                image_access_back.visibility = View.VISIBLE
                linearLayout.visibility = View.GONE
            }
        }
    }

    private fun supportFragmentManager(fragment: Fragment, isToBackStack: Boolean = true) {
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        transaction.add(R.id.frame_holder_access, fragment)
        if (isToBackStack) {
            transaction.addToBackStack("")
        }
        transaction.commit()
    }

    fun onClickBack(view: View) {
        this.onBackPressed()
    }

    fun onClickIntent(view: View) {
        KeyboardUtils.hideSoftInput(view)
        when (view.id) {
            //跳转忘记密码页面
            R.id.text_login_pwd_forget -> {

            }
            //跳转验证码登录页面
            R.id.text_login_pwd_check -> supportFragmentManager(
                AccessLoginCheckFragment.newInstance()
            )
            //跳转账号密码页面
            R.id.text_login_check_pwd -> {
                onBackPressed()
            }
            //跳转注册页面
            R.id.text_login_pwd_register -> supportFragmentManager(
                AccessRegisterFragment.newInstance()
            )
            //跳转注册页面
            R.id.text_login_check_register -> supportFragmentManager(
                AccessRegisterFragment.newInstance()
            )
        }
    }

    fun onClickNetwork(view: View) {
        when (view.id) {
            R.id.text_login_pwd_login -> {
                //登录
                LoadingDialog.show(context = this, isBlurBackground = true)
            }
            //获取验证码
            R.id.text_login_check_get_captcha -> supportFragmentManager(
                AccessCheckCodeFragment.newInstance()
            )
        }
    }
}